package com.tanhua.dubbo.api.impl;


import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.RecommendUserApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    // db.recommend_user.find({userId:1}).sort({score:-1}).limit(1)
    public RecommendUser queryWithMaxScore(Long userId) {
        // 创建query查询对象
        Query query = new Query(Criteria.where("userId").is(userId));
        // 指定排序参数
        query.with(Sort.by(Sort.Direction.DESC, "score"));
        // 指定分页参数
        query.limit(1);

        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        return recommendUser;
    }

    @Override
    public PageResult queryRecommendation(Long userId, Integer page, Integer pagesize) {
        // 创建query查询对象
        Query query = new Query(Criteria.where("userId").is(userId));
        // 指定排序参数
        query.with(Sort.by(Sort.Direction.DESC, "date"));
        // 指定分页参数
        query.limit(pagesize).skip((page - 1) * pagesize);

        // 获取当前页的数据
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);

        // 获取总条数
        long count = mongoTemplate.count(query, RecommendUser.class);

        return new PageResult(page, pagesize, (int) count, recommendUserList);
    }

    @Override
    public Long queryScore(Long userId, Long id) {
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("recommendUserId").is(id));
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);

        // 如果用户和佳人没有缘分
        if (recommendUserList == null || recommendUserList.size() == 0) {
            return 80l;
        }
        return recommendUserList.get(0).getScore().longValue();
    }

    /**
     * 根据用户的Id查询推荐用户
     *
     * @param userId 用户Id
     * @return
     */
    @Override
    public List<RecommendUser> findRecommendUser(Long userId) {
        // 1.查询条件
        Query query = new Query(Criteria.where("userId").is(userId));
        // 2.调用MongoDB的方法
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        return recommendUserList;
    }

    /**
     * 根据当前用户Id删除推荐用户
     *
     * @param userId          用户Id
     * @param recommendUserId 推荐用户Id
     */
    @Override
    public void deleteRecommendUserId(Long userId, Long recommendUserId) {
        // 1.删除条件
        Query query = new Query(Criteria.where("recommendUserId").is(recommendUserId)
        .and("userId").is(userId));
        // 2.根据用户Id删除推荐好友Id
        mongoTemplate.remove(query, RecommendUser.class);
    }


}
